package champPackage;
import java.sql.SQLException;
import java.util.*;

public class Championship {
	private DBManager manager;
	private User organizer;
	private int id;
	private ArrayList<Team> teams=new ArrayList<Team>();
	private ArrayList<Match> completedMatches;
	private String name;
	private Date startDate;
	private Date endDate;
	
	/**
	 * The constructor for a Championship class. Initializes a Championship class object, which will keep information about the whole championship.
	 * @param organizer The User object representing the organizer.
	 * 
	 */
	public Championship(User organizer){//konstruqtorshi id mienicha
		manager=DBManager.getInstance();
		this.organizer=organizer;
		/*
		try {
			id=manager.getNewChampionshipId();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			manager.addNewChampionship(id, organizer.getID());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		teams=new ArrayList<Team>();
		completedMatches=new ArrayList<Match>();
		*/
	}
	
	
	
	public void addChampionshipTeam(Team t){
		teams.add(t);
	}
	
	public void setChampionshipId(int ID){
		id=ID;
	}
	
	/**
	 * Sets the name (title) to the championship.
	 * @param name the desired name of the championship.
	 * @throws SQLException 
	 */
	public void setChampionshipName(String name) throws SQLException{
		this.name=name;
	}
	
	/**
	 * Sets the start date for the current championship.
	 * @param start The desired date for the start.
	 */
	public void setChampionshipStartDate(Date start){
		startDate=start;
	}
	
	/**
	 * Sets the end date for the current championship.
	 * @param desired time for the end.
	 */
	public void setChampionshipEndtDate(Date end){
		endDate=end;
	}
	
	/**
	 * Completes the championship, i.e. marks the championship as completed.
	 */
	public void completeChampionship(){
		isCompleted=true;
	}
	
	/**
	 * Returns the iterator of all the the Match objects that have been held in this championship.
	 * @return The iterator of all the played matches.
	 */
	public Iterator<Match> getPlayedMatches(){
		return completedMatches.iterator(); 
	}
	
	/**
	 * Returns the id of the current championship.
	 */
	public int getId(){
		return id; 
	}
	
	/**
	 * Returns the name (title) of the current championship.
	 * @return The name of the championship.
	 */
	public String getChampionshipName(){
		return name;
	}
	
	/**
	 * Returns the start date of the championship, if it has been indicated by the organizer. Otherwise returns null.
	 * @return The start date of the championship, or null if it's not indicated.
	 */
	public Date startDate(){
		return startDate; 
	}
	
	/**
	 * Returns the end date of the current championship if it has been indicated by the organizer. Otherwise returns null.
	 * @return
	 */
	public Date endDate(){
		return endDate;
	}
	
	/**
	 * Returns the iterator of all the Team objects that have played in the current championship.
	 * @return
	 */
	public Iterator<Team> getPlayerTeams(){
		return teams.iterator(); 
	}
	
	/**
	 * Returns the file name of the current championship logo. If it has not been indicated by the organizer, returns null.
	 * @return The file name of the logo, or null if it's not indicated.
	 */
	public String getLogo(){
		return ""; // STUB
	}
	
	/**
	 * Returns the number of the teams in the championship.
	 * @return The number of participating teams.
	 */
	public int numberOfTeams(){
		return teams.size(); // STUB
	}
	
	/**
	 * Adds the team to the list of the championship participants.
	 * @param t The team to be added.
	 */
	public void addTeam(Team t){
		teams.add(t);
		
	}
	
	/**
	 * Removes the team from the list of the championship participants.
	 * @param t The team to be removed.
	 */
	public void removeTeam(Team t){
		for(int i=0;i<teams.size();i++){
			if(t.getId()==teams.get(i).getId()){
				teams.remove(i);
				break;
			}
		}
	}
	
	/**
	 * Returns the winner of the current championship.
	 * @return The winner team of the championship.
	 */
	public Team championTeam(){
		return null; // STUB
	}
	
	/**
	 * Returns whether the current championship is finished.
	 * @return Whether the current championship is finished.
	 */
	public boolean isFinished(){
		return isCompleted; //STUB
	}
	
	/**
	 * Adds the completed match to the list of all the completed matches in the current championship.
	 * @param m The Match object to be added.
	 */
	public void addCompletedMatch(Match m){
		completedMatches.add(m);
	}
	
	// This variable keeps information whether the championship is already finished.
	private boolean isCompleted = false;
	
}
